import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getwidget/getwidget.dart';
import 'package:meal/constant/sizes.dart';
import 'package:meal/utils/colors.dart';
import 'package:meal/utils/styles.dart';
import 'package:meal/utils/tools.dart';

///
/// 使用时间
///
class GroupUseTime extends StatefulWidget {
  const GroupUseTime({Key? key}) : super(key: key);

  @override
  State<GroupUseTime> createState() => _GroupUseTimeState();
}

class _GroupUseTimeState extends State<GroupUseTime> with SingleTickerProviderStateMixin {
  List<String> week = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日'];

  List<int> weekIndex = [];

  int all = -1;

  late Animation<double> animation;

  late AnimationController controller;

  String title = '使用时间';

  @override
  void initState() {
    super.initState();
    if (Get.arguments != null) {
      title = Get.arguments as String;
    }
    controller = AnimationController(duration: const Duration(milliseconds: 500), vsync: this);
    animation = CurvedAnimation(parent: controller, curve: Curves.linear);
    controller.reverse();
  }

  @override
  void dispose() {
    super.dispose();
    controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar:
          ToolsUtils.getDefaultBrnAppBar(title, actions: ToolsUtils.actions('确定', pressed: () {})),
      body: SingleChildScrollView(
        child: Column(
          children: [
            Container(
              padding: const EdgeInsets.symmetric(horizontal: Sizes.default20),
              height: Sizes.default50,
              alignment: Alignment.centerLeft,
              child: Text(
                '生效星期',
                style: StylesUtils.customTextStyle(
                    fontSize: Sizes.default15, color: ColorsUtils.color363),
              ),
            ),
            Container(
                alignment: Alignment.centerLeft,
                padding: const EdgeInsets.symmetric(horizontal: Sizes.default20),
                child: LayoutBuilder(builder: (BuildContext ctx, BoxConstraints constraints) {
                  double spacing = 20;
                  var w = (constraints.maxWidth - spacing) / 2;
                  return Wrap(
                    spacing: spacing,
                    runSpacing: 30,
                    children: week
                        .asMap()
                        .map((index, value) => MapEntry(
                            index,
                            InkWell(
                              onTap: () {
                                setState(() {
                                  if (weekIndex.contains(index)) {
                                    weekIndex.remove(index);
                                    return;
                                  }
                                  weekIndex.add(index);
                                });
                              },
                              child: Container(
                                constraints:
                                    BoxConstraints.expand(width: w, height: Sizes.default45),
                                alignment: Alignment.center,
                                decoration: changeBoxDecoration(weekIndex.contains(index)),
                                child: Text(
                                  value,
                                  style: StylesUtils.customTextStyle(
                                      fontSize: Sizes.default15,
                                      color: weekIndex.contains(index)
                                          ? ColorsUtils.colorFf6
                                          : ColorsUtils.color797),
                                ),
                              ),
                            )))
                        .values
                        .toList(),
                  );
                })),
            Container(
              padding: const EdgeInsets.symmetric(horizontal: Sizes.default20),
              height: Sizes.default50,
              alignment: Alignment.centerLeft,
              child: Text(
                '生效时间',
                style: StylesUtils.customTextStyle(
                    fontSize: Sizes.default15, color: ColorsUtils.color363),
              ),
            ),
            Container(
              alignment: Alignment.centerLeft,
              padding: const EdgeInsets.symmetric(horizontal: Sizes.default20),
              height: Sizes.default60,
              child: Row(
                children: [
                  Expanded(
                      child: Container(
                    margin: const EdgeInsets.only(right: Sizes.default10),
                    child: InkWell(
                      onTap: () {
                        setState(() {
                          all = 0;
                          controller.reverse();
                        });
                      },
                      child: Container(
                        height: Sizes.default45,
                        alignment: Alignment.center,
                        decoration: changeBoxDecoration(all == 0),
                        child: Text(
                          '全天',
                          style: StylesUtils.customTextStyle(
                              fontSize: Sizes.default15,
                              color: all == 0 ? ColorsUtils.colorFf6 : ColorsUtils.color797),
                        ),
                      ),
                    ),
                  )),
                  Expanded(
                      child: Container(
                    margin: const EdgeInsets.only(left: Sizes.default10),
                    child: InkWell(
                      onTap: () {
                        setState(() {
                          all = 1;
                          controller.forward();
                        });
                      },
                      child: Container(
                        height: Sizes.default45,
                        alignment: Alignment.center,
                        decoration: changeBoxDecoration(all == 1),
                        child: Text(
                          '自定义',
                          style: StylesUtils.customTextStyle(
                              fontSize: Sizes.default15,
                              color: all == 1 ? ColorsUtils.colorFf6 : ColorsUtils.color797),
                        ),
                      ),
                    ),
                  ))
                ],
              ),
            ),
            GFAnimation(
                type: GFAnimationType.scaleTransition,
                scaleAnimation: animation,
                child: Container(
                  padding: const EdgeInsets.symmetric(horizontal: Sizes.default20),
                  child: Column(
                    children: [
                      Container(
                        alignment: Alignment.centerLeft,
                        height: Sizes.default50,
                        margin: const EdgeInsets.only(bottom: Sizes.default15),
                        child: Row(
                          children: [
                            Expanded(
                                child: Container(
                              decoration: BoxDecoration(
                                  color: ColorsUtils.rgba(247, 247, 247, 1),
                                  borderRadius:
                                      const BorderRadius.all(Radius.circular(Sizes.default10))),
                              child: TextField(
                                style: StylesUtils.customTextStyle(
                                    color: ColorsUtils.color363, fontSize: Sizes.default15),
                                textAlign: TextAlign.center,
                                decoration: InputDecoration(
                                    border: const OutlineInputBorder(
                                      borderSide: BorderSide.none,
                                    ),
                                    isCollapsed: true,
                                    filled: true,
                                    fillColor: Colors.transparent,
                                    contentPadding: const EdgeInsets.symmetric(
                                        horizontal: Sizes.default10, vertical: Sizes.default10),
                                    constraints: const BoxConstraints(minHeight: Sizes.default45),
                                    hintText: '请输入开始时间',
                                    hintStyle: StylesUtils.customTextStyle(
                                        fontSize: Sizes.default14, color: ColorsUtils.color999)),
                                keyboardType: TextInputType.text,
                                onChanged: (String content) {},
                              ),
                            )),
                            Container(
                              alignment: Alignment.center,
                              width: Sizes.default30,
                              child: Text(
                                '至',
                                style: StylesUtils.customTextStyle(
                                    color: ColorsUtils.color333, fontSize: Sizes.default15),
                              ),
                            ),
                            Expanded(
                                child: Container(
                              decoration: BoxDecoration(
                                  color: ColorsUtils.rgba(247, 247, 247, 1),
                                  borderRadius:
                                      const BorderRadius.all(Radius.circular(Sizes.default10))),
                              child: TextField(
                                style: StylesUtils.customTextStyle(
                                    color: ColorsUtils.color363, fontSize: Sizes.default15),
                                textAlign: TextAlign.center,
                                decoration: InputDecoration(
                                    border: const OutlineInputBorder(
                                      borderSide: BorderSide.none,
                                    ),
                                    isCollapsed: true,
                                    filled: true,
                                    fillColor: Colors.transparent,
                                    contentPadding: const EdgeInsets.symmetric(
                                        horizontal: Sizes.default10, vertical: Sizes.default10),
                                    constraints: const BoxConstraints(minHeight: Sizes.default45),
                                    hintText: '请输入结束时间',
                                    hintStyle: StylesUtils.customTextStyle(
                                        fontSize: Sizes.default14, color: ColorsUtils.color999)),
                                keyboardType: TextInputType.text,
                                onChanged: (String content) {},
                              ),
                            )),
                          ],
                        ),
                      ),
                      Container(
                        alignment: Alignment.centerLeft,
                        height: Sizes.default50,
                        margin: const EdgeInsets.only(bottom: Sizes.default15),
                        child: Row(
                          children: [
                            Expanded(
                                child: Container(
                              decoration: BoxDecoration(
                                  color: ColorsUtils.rgba(247, 247, 247, 1),
                                  borderRadius:
                                      const BorderRadius.all(Radius.circular(Sizes.default10))),
                              child: TextField(
                                style: StylesUtils.customTextStyle(
                                    color: ColorsUtils.color363, fontSize: Sizes.default15),
                                textAlign: TextAlign.center,
                                decoration: InputDecoration(
                                    border: const OutlineInputBorder(
                                      borderSide: BorderSide.none,
                                    ),
                                    isCollapsed: true,
                                    filled: true,
                                    fillColor: Colors.transparent,
                                    contentPadding: const EdgeInsets.symmetric(
                                        horizontal: Sizes.default10, vertical: Sizes.default10),
                                    constraints: const BoxConstraints(minHeight: Sizes.default45),
                                    hintText: '请输入开始时间',
                                    hintStyle: StylesUtils.customTextStyle(
                                        fontSize: Sizes.default14, color: ColorsUtils.color999)),
                                keyboardType: TextInputType.text,
                                onChanged: (String content) {},
                              ),
                            )),
                            Container(
                              alignment: Alignment.center,
                              width: Sizes.default30,
                              child: Text(
                                '至',
                                style: StylesUtils.customTextStyle(
                                    color: ColorsUtils.color333, fontSize: Sizes.default15),
                              ),
                            ),
                            Expanded(
                                child: Container(
                              decoration: BoxDecoration(
                                  color: ColorsUtils.rgba(247, 247, 247, 1),
                                  borderRadius:
                                      const BorderRadius.all(Radius.circular(Sizes.default10))),
                              child: TextField(
                                style: StylesUtils.customTextStyle(
                                    color: ColorsUtils.color363, fontSize: Sizes.default15),
                                textAlign: TextAlign.center,
                                decoration: InputDecoration(
                                    border: const OutlineInputBorder(
                                      borderSide: BorderSide.none,
                                    ),
                                    isCollapsed: true,
                                    filled: true,
                                    fillColor: Colors.transparent,
                                    contentPadding: const EdgeInsets.symmetric(
                                        horizontal: Sizes.default10, vertical: Sizes.default10),
                                    constraints: const BoxConstraints(minHeight: Sizes.default45),
                                    hintText: '请输入结束时间',
                                    hintStyle: StylesUtils.customTextStyle(
                                        fontSize: Sizes.default14, color: ColorsUtils.color999)),
                                keyboardType: TextInputType.text,
                                onChanged: (String content) {},
                              ),
                            )),
                          ],
                        ),
                      ),
                      Container(
                        alignment: Alignment.centerLeft,
                        height: Sizes.default50,
                        margin: const EdgeInsets.only(bottom: Sizes.default15),
                        child: Row(
                          children: [
                            Expanded(
                                child: Container(
                              decoration: BoxDecoration(
                                  color: ColorsUtils.rgba(247, 247, 247, 1),
                                  borderRadius:
                                      const BorderRadius.all(Radius.circular(Sizes.default10))),
                              child: TextField(
                                style: StylesUtils.customTextStyle(
                                    color: ColorsUtils.color363, fontSize: Sizes.default15),
                                textAlign: TextAlign.center,
                                decoration: InputDecoration(
                                    border: const OutlineInputBorder(
                                      borderSide: BorderSide.none,
                                    ),
                                    isCollapsed: true,
                                    filled: true,
                                    fillColor: Colors.transparent,
                                    contentPadding: const EdgeInsets.symmetric(
                                        horizontal: Sizes.default10, vertical: Sizes.default10),
                                    constraints: const BoxConstraints(minHeight: Sizes.default45),
                                    hintText: '请输入开始时间',
                                    hintStyle: StylesUtils.customTextStyle(
                                        fontSize: Sizes.default14, color: ColorsUtils.color999)),
                                keyboardType: TextInputType.text,
                                onChanged: (String content) {},
                              ),
                            )),
                            Container(
                              alignment: Alignment.center,
                              width: Sizes.default30,
                              child: Text(
                                '至',
                                style: StylesUtils.customTextStyle(
                                    color: ColorsUtils.color333, fontSize: Sizes.default15),
                              ),
                            ),
                            Expanded(
                                child: Container(
                              decoration: BoxDecoration(
                                  color: ColorsUtils.rgba(247, 247, 247, 1),
                                  borderRadius:
                                      const BorderRadius.all(Radius.circular(Sizes.default10))),
                              child: TextField(
                                style: StylesUtils.customTextStyle(
                                    color: ColorsUtils.color363, fontSize: Sizes.default15),
                                textAlign: TextAlign.center,
                                decoration: InputDecoration(
                                    border: const OutlineInputBorder(
                                      borderSide: BorderSide.none,
                                    ),
                                    isCollapsed: true,
                                    filled: true,
                                    fillColor: Colors.transparent,
                                    contentPadding: const EdgeInsets.symmetric(
                                        horizontal: Sizes.default10, vertical: Sizes.default10),
                                    constraints: const BoxConstraints(minHeight: Sizes.default45),
                                    hintText: '请输入结束时间',
                                    hintStyle: StylesUtils.customTextStyle(
                                        fontSize: Sizes.default14, color: ColorsUtils.color999)),
                                keyboardType: TextInputType.text,
                                onChanged: (String content) {},
                              ),
                            )),
                          ],
                        ),
                      ),
                    ],
                  ),
                ))
          ],
        ),
      ),
    );
  }

  BoxDecoration changeBoxDecoration(bool isActive) {
    if (isActive) {
      return BoxDecoration(
          color: ColorsUtils.rgba(255, 238, 238, 1),
          borderRadius: BorderRadius.circular(Sizes.default15),
          border: Border.all(color: ColorsUtils.rgba(255, 110, 83, 1)));
    }
    return BoxDecoration(
      color: ColorsUtils.rgba(245, 245, 245, 1),
      borderRadius: BorderRadius.circular(Sizes.default15),
    );
  }
}
